#include "cgePainterOgl.h"
#include "cgeCanvasWin.h"

namespace cge
{
	namespace gfx
	{
		PainterOgl::PainterOgl()
			: mFixedPipeLine(true)
		{
		}

		PainterOgl::~PainterOgl()
		{
		}
		
		void PainterOgl::Initialize(Canvas* canvas)
		{
#ifdef WIN32
			CanvasWin* cvw = (CanvasWin*)canvas;

			mDevContext = GetDC(cvw->GetHWND());
			PIXELFORMATDESCRIPTOR pfd=					
			{
				sizeof(PIXELFORMATDESCRIPTOR),	// Size Of This Pixel Format Descriptor
				1,								// Version Number
				PFD_DRAW_TO_WINDOW |			// Format Must Support Window
				PFD_SUPPORT_OPENGL |			// Format Must Support OpenGL
				PFD_DOUBLEBUFFER,				// Must Support Double Buffering
				PFD_TYPE_RGBA,					// Request An RGBA Format
				24,								// Select Our Color Depth
				0, 0, 0, 0, 0, 0,				// Color Bits Ignored
				0,								// No Alpha Buffer
				0,								// Shift Bit Ignored
				0,								// No Accumulation Buffer
				0, 0, 0, 0,						// Accumulation Bits Ignored
				24,								// 24Bit Z-Buffer (Depth Buffer)
				0,								// No Stencil Buffer
				0,								// No Auxiliary Buffer
				PFD_MAIN_PLANE,					// Main Drawing Layer
				0,								// Reserved
				0, 0, 0							// Layer Masks Ignored
			};

			s32 pfdId = ChoosePixelFormat(mDevContext,&pfd);
			if(!pfdId)
				return;

			if(!SetPixelFormat(mDevContext,pfdId, &pfd))
				return;

			mOglHandle = wglCreateContext(mDevContext);
			if(!mOglHandle)
				return;

			if(!wglMakeCurrent(mDevContext, mOglHandle))
				return;
#endif
		}

		void PainterOgl::BeginScene()
		{
			glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
			if(mFixedPipeLine)
				glLoadIdentity();
		}

		void PainterOgl::EndScene()
		{
#ifdef WIN32
			SwapBuffers(mDevContext);
#endif
		}
	}
}